package app.widget;

import android.content.Context;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.style.RelativeSizeSpan;
import android.util.AttributeSet;
import android.view.WindowManager;
import android.widget.TextView;
import app.log.APPLog;


public class MatchTextView extends TextView {

	/**
	 * mWindowManager
	 */
	private WindowManager mWindowManager;

	/**
	 * 是否已经Layout
	 */
	private boolean isLayout = false;

	/**
	 * 是否文字改变
	 */
	private boolean isTextChanged = false;

	public MatchTextView(Context context) {
		super(context);
		init(context);
	}

	public MatchTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public MatchTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {
		mWindowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
	}

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//		PABankLog.i("MatchTextView",
//				"this is " + this + "onMeasure height" + this.getHeight());
//		PABankLog.i("MatchTextView",
//				"this is " + this + "onMeasure width" + this.getWidth());
	}

	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		APPLog.i("MatchTextView", "this is " + this + "onLayout");
//		PABankLog.i("MatchTextView",
//				"this is " + this + "onLayout height" + this.getHeight());
//		PABankLog.i("MatchTextView",
//				"this is " + this + "onLayout width" + this.getWidth());
		isLayout = true;
		isTextChanged = true;
		setTextSize();
		isTextChanged = false;
	}

	protected void onTextChanged(CharSequence text, int start, int before,
			int after) {

		APPLog.i("MatchTextView", "this is " + this + "onTextChanged text:" + text);
		// PABankLog.i("MatchTextView", "this is " + this + "onTextChanged height"
		// + this.getHeight());
		// PABankLog.i("MatchTextView",
		// "this is " + this + "onTextChanged width" + this.getWidth());

		if (isLayout && !isTextChanged) {
			isTextChanged = true;
			setTextSize();
			isTextChanged = false;
		}

	}

	/**
	 * 设置字体大小
	 */
	public void setTextSize() {
//		DisplayMetrics displayMetrics = new DisplayMetrics();
//		mWindowManager.getDefaultDisplay().getMetrics(displayMetrics);
//		float density = displayMetrics.density;
//		PABankLog.i("MatchTextView", "this is " + this + "setTextSize density"
//				+ density);
		// int h = this.getHeight();
		int w = this.getWidth() - getPaddingLeft() - getPaddingRight();
		// Log.i("MatchTextView", "this is " + this +"setTextSize height" + h);
		APPLog.i("MatchTextView", "this is " + this + "setTextSize width" + w);

//		float size = this.getTextSize();
//		int length = this.getText().length();
//		PABankLog.i("MatchTextView", "this is " + this + "setTextSize size" + size);
//		PABankLog.i("MatchTextView", "this is " + this + "setTextSize length"
//				+ length);
		TextPaint paint = this.getPaint();
		float textLength = paint.measureText(this.getText().toString()) + 15;
		APPLog.i("MatchTextView", "this is " + this + "setTextSize textlenght"
				+ textLength);
		SpannableString sps = new SpannableString(this.getText().toString());
		if (textLength > w) {
			float x = w / textLength;
			APPLog.i("MatchTextView", "this is " + this + "setTextSize x" + x);
			sps.setSpan(new RelativeSizeSpan(x), 0, sps.length(), 0);
		}
		this.setText(sps);
		// this.setTextSize(25f);

	}

	@Override
	public void setTextSize(int unit, float size) {
		super.setTextSize(unit, size);
//		PABankLog.i("MatchTextView", "this is " + this
//				+ "super setTextSize(int unit, float size) unit" + unit);
//		PABankLog.i("MatchTextView", "this is " + this
//				+ "super setTextSize(int unit, float size) size" + size);
	}
	//
	// /**
	// * 设置文本并设置大小
	// *
	// * @param text 文本
	// */
	// public void setTextAndSetSize(CharSequence text) {
	// super.setText(text);
	// PABankLog.i("MatchTextView", "this is " + this + "setTextAndSetSize text:"
	// + text);
	// PABankLog.i("MatchTextView",
	// "this is " + this + "onLayout height" + this.getHeight());
	// PABankLog.i("MatchTextView",
	// "this is " + this + "onLayout width" + this.getWidth());
	// setTextSize();
	// }

}
